package com.xuzimian.globaldemo.spring.core.bean.warpper;

import org.junit.Test;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.PropertyValue;

/**
 * @program: global-demo
 * @description: BeanWrapper是Spring提供的一个用来操作JavaBean属性的工具，使用它可以直接修改一个对象的属性。
 * @author: xzm
 * @create: 2019-05-28 10:51
 **/
public class BeanWrapperDemo {


    @Test
    public  void test() {
        User user = new User();
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(user);
        bw.setPropertyValue("userName", "张三");
        System.out.println(user.getUserName()); //输出张三
        PropertyValue value = new PropertyValue("userName", "李四");
        bw.setPropertyValue(value);
        System.out.println(user.getUserName()); //输出李四
    }


    static  class User {
        String userName;

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }
    }
}
